package cn.stylefeng.guns.modular.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.UseCollect;
import cn.stylefeng.guns.modular.business.enums.UseCollectExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.UseCollectMapper;
import cn.stylefeng.guns.modular.business.pojo.UseCollectRequest;
import cn.stylefeng.guns.modular.business.service.UseCollectService;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户收藏业务实现层
 *
 * @author zhangyaowen
 * @date 2021/11/17 15:51
 */
@Service
public class UseCollectServiceImpl extends ServiceImpl<UseCollectMapper, UseCollect> implements UseCollectService {

    @Resource
    private LoginUserApi loginUserApi;
    
	@Override
    public String add(String collectTitle, String collectType) {

        HttpServletRequest request = HttpServletUtil.getRequest();
        Long userid;
        LoginUser loginUser = new LoginUser();
        if (ObjectUtil.isNotNull(request)) {
            loginUser = loginUserApi.getLoginUser();
            userid = loginUser.getUserId();
        } else {
            throw new ServiceException(UseCollectExceptionEnum.REQUEST_EMPTY);
        }
        UseCollectRequest useCollectRequest = new UseCollectRequest();
        useCollectRequest.setCollectTitle(collectTitle);
        useCollectRequest.setCollectType(collectType);
        useCollectRequest.setCollectUrl(request.getRequestURI());
        useCollectRequest.setCollectTime(DateUtil.now());
        useCollectRequest.setUserID(userid);
//        LoginUser user = LoginContextHolder.me().getSysLoginUserUpToDate();
        useCollectRequest.setUserName(loginUser.getAccount());

        LambdaQueryWrapper<UseCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(collectTitle), UseCollect::getCollectTitle, collectTitle);
        wrapper.eq(ObjectUtil.isNotEmpty(userid), UseCollect::getUserID, userid);
        wrapper.eq(ObjectUtil.isNotEmpty(useCollectRequest.getCollectUrl()), UseCollect::getCollectUrl, useCollectRequest.getCollectUrl());

        List<UseCollect> useCollectList = this.list(wrapper);
        if(useCollectList.isEmpty()) {
            UseCollect useCollect = new UseCollect();
            BeanUtil.copyProperties(useCollectRequest, useCollect);
            this.save(useCollect);
            LambdaQueryWrapper<UseCollect> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(ObjectUtil.isNotEmpty(userid), UseCollect::getUserID, userid);
            wrapper1.eq(ObjectUtil.isNotEmpty(useCollectRequest.getCollectUrl()), UseCollect::getCollectUrl, useCollectRequest.getCollectUrl());

            List<UseCollect> useCollectList1 = this.list(wrapper1);

            return useCollectList1.get(0).getId().toString();

        }else{
            throw new ServiceException(UseCollectExceptionEnum.USECOLLECT_TYPE_EXISTED);
        }
    }

    @Override
    public void del(UseCollectRequest useCollectRequest) {
        UseCollect useCollect = this.queryUseCollect(useCollectRequest);
        this.removeById(useCollect.getId());
    }

    @Override
    public void edit(UseCollectRequest useCollectRequest) {
        UseCollect useCollect = this.queryUseCollect(useCollectRequest);
        BeanUtil.copyProperties(useCollectRequest, useCollect);
        this.updateById(useCollect);
    }

    @Override
    public UseCollect detail(UseCollectRequest useCollectRequest) {
        return this.queryUseCollect(useCollectRequest);
    }

    @Override
    public PageResult<Map<String, Object>> findPage(UseCollectRequest useCollectRequest) {
        HttpServletRequest request = HttpServletUtil.getRequest();
        Long userid;
        if (ObjectUtil.isNotNull(request)) {

            LoginUser loginUser = loginUserApi.getLoginUser();
            userid = loginUser.getUserId();
        } else {
            throw new ServiceException(UseCollectExceptionEnum.REQUEST_EMPTY);
        }
        if(userid != -1l || userid != 0l) {
//            LambdaQueryWrapper<UseCollect> wrapper = createWrapper(useCollectRequest);
//            Page<UseCollect> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
//            return PageResultFactory.createPageResult(sysRolePage);
            LambdaQueryWrapper<UseCollect> queryWrapper = new LambdaQueryWrapper<>();
            String CollectTitle = useCollectRequest.getCollectTitle();
            queryWrapper.like(ObjectUtil.isNotEmpty(CollectTitle), UseCollect::getCollectTitle, CollectTitle)
                    .eq(ObjectUtil.isNotEmpty(userid), UseCollect::getUserID, userid)
                    .select(UseCollect::getCollectTitle,UseCollect::getCollectType,UseCollect::getCollectTime,UseCollect::getCollectUrl)
                    .orderByDesc(UseCollect::getCollectTime);
            Page<UseCollect> sysRolePage = this.page(PageFactory.defaultPage(), queryWrapper);

            PageResult<UseCollect> UseCollectPageResult = PageResultFactory.createPageResult(sysRolePage);

            List<UseCollect> list = UseCollectPageResult.getRows();
            PageResult<Map<String, Object>> pageResult = new PageResult<>();
            List<Map<String,Object>> mapList = new ArrayList<>();
            for(UseCollect UseCollect : list){
                Map<String, Object> map = new HashMap<String, Object>();
//            map = JSONObject.parseObject(JSONObject.toJSONString(projectScience), Map.class);
                map.put("collectTitle",UseCollect.getCollectTitle());
                map.put("collectType",UseCollect.getCollectType());
                map.put("collectTime",UseCollect.getCollectTime());
                map.put("collectUrl",UseCollect.getCollectUrl());
                mapList.add(map);
            }
            pageResult.setRows(mapList);
            pageResult.setPageNo(UseCollectPageResult.getPageNo());
            pageResult.setTotalRows(UseCollectPageResult.getTotalRows());
            pageResult.setPageSize(UseCollectPageResult.getPageSize());
            pageResult.setTotalPage(UseCollectPageResult.getTotalPage());
            return  pageResult;
        }else{
            throw new ServiceException(UseCollectExceptionEnum.USECOLLECT_NOT_EXISTED);
        }
    }

    @Override
    public List<UseCollect> findList(UseCollectRequest useCollectRequest) {
        LambdaQueryWrapper<UseCollect> wrapper = this.createWrapper(useCollectRequest);
        return this.list(wrapper);
    }


    @Override
    public String findCollectType() {
        HttpServletRequest request = HttpServletUtil.getRequest();
        Long userid;
        if (ObjectUtil.isNotNull(request)) {

            LoginUser loginUser = loginUserApi.getLoginUser();
            userid = loginUser.getUserId();
        } else {
            throw new ServiceException(UseCollectExceptionEnum.REQUEST_EMPTY);
        }
        LambdaQueryWrapper<UseCollect> wrapper = new LambdaQueryWrapper<>();

        String collectUrl = request.getRequestURI();

        wrapper.eq(ObjectUtil.isNotEmpty(userid), UseCollect::getUserID, userid);
        wrapper.eq(ObjectUtil.isNotEmpty(collectUrl), UseCollect::getCollectUrl, collectUrl);

        List<UseCollect> useCollectList = this.list(wrapper);
        if(!useCollectList.isEmpty()){
            return useCollectList.get(0).getId().toString();
        }else {
            return null;
        }

    }

    /**
     * 获取信息
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    private UseCollect queryUseCollect(UseCollectRequest useCollectRequest) {
        UseCollect useCollect = this.getById(useCollectRequest.getId());
        if (ObjectUtil.isEmpty(useCollect)) {
            throw new ServiceException(UseCollectExceptionEnum.USECOLLECT_NOT_EXISTED);
        }
        return useCollect;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    private LambdaQueryWrapper<UseCollect> createWrapper(UseCollectRequest useCollectRequest) {
        LambdaQueryWrapper<UseCollect> queryWrapper = new LambdaQueryWrapper<>();

        Long id = useCollectRequest.getId();
        String collectTitle = useCollectRequest.getCollectTitle();
        String userName = useCollectRequest.getUserName();
        String collectType = useCollectRequest.getCollectType();
        String collectTime = useCollectRequest.getCollectTime();
        String collectUrl = useCollectRequest.getCollectUrl();
        String collectZu = useCollectRequest.getCollectZu();

        queryWrapper.eq(ObjectUtil.isNotNull(id), UseCollect::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(collectTitle), UseCollect::getCollectTitle, collectTitle);
        queryWrapper.like(ObjectUtil.isNotEmpty(userName), UseCollect::getUserName, userName);
        queryWrapper.like(ObjectUtil.isNotEmpty(collectType), UseCollect::getCollectType, collectType);
        queryWrapper.eq(ObjectUtil.isNotNull(collectTime), UseCollect::getCollectTime, collectTime);
        queryWrapper.like(ObjectUtil.isNotEmpty(collectUrl), UseCollect::getCollectUrl, collectUrl);
        queryWrapper.like(ObjectUtil.isNotEmpty(collectZu), UseCollect::getCollectZu, collectZu);

        return queryWrapper;
    }

}